/*	BuildGoliath class definition.
 *
*/

#include "BuildGoliath.h"

void BuildGoliath::Enter()
{
	DEBUG_PRINT("BuildGoliath(%p) %s entering\n", this, GetName().c_str());

}

void BuildGoliath::Execute()
{
	DEBUG_PRINT("BuildGoliath(%p) %s executing\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>* factoryList;
	BWAPI::Unit* fac = NULL;
	for(std::list<Base*>::iterator base = bases->begin(); base != bases->end(); base++)
	{
		factoryList = (*base)->GetBuildingsByType(BWAPI::UnitTypes::Terran_Factory);
		if(factoryList)
		{
			for(std::list<BWAPI::Unit*>::iterator it = factoryList->begin(); it != factoryList->end(); it++)
			{
				if (!(*it)->isTraining() && !(*it)->isBeingConstructed())
					fac = *it;
			}
			if (fac) 
			{
				BB->SpendResources(BWAPI::UnitTypes::Terran_Goliath);
				fac->train(BWAPI::UnitTypes::Terran_Goliath);
			}
		}
	}
	control->FinishWithSuccess();
}

void BuildGoliath::Exit()
{
	DEBUG_PRINT("BuildGoliath(%p) %s exiting\n", this, GetName().c_str());

}

bool BuildGoliath::CheckConditions()
{
	DEBUG_PRINT("BuildGoliath(%p) %s checking conditions\n", this, GetName().c_str());
	int enemyAir = BB->NumberOfEnemyAirUnits();
	std::list<BWAPI::Unit*>* goliaths = BB->GetUnitsByTypeGeneral(BWAPI::UnitTypes::Terran_Goliath);
	int numberGoliath = (goliaths) ? goliaths->size() : 0;
	if(enemyAir * 2 > numberGoliath)
	{
		if(BB->isBuildUnit(BWAPI::UnitTypes::Terran_Goliath) )
		{
			DEBUG_PRINT("BuildGoliath(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
			
			return true;
		}
	}
	return false;
}